package BigTest;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;

public class test04 {
    public static void main(String[] args) throws IOException {
        String src = "allname.txt";
        String back = "back.txt";
        ArrayList<String> list = readLine(src);
        if (list.size() == 0) {
            list = readLine(back);
            writeLine(src,list,false);
            new File(back).delete();
        }
        Collections.shuffle(list);
        String remove = list.remove(0);
        System.out.println(remove.split("-")[0] + " 被点到了");
        writeLine(src, list, false);
        writeLine(back, remove, true);
    }

    private static void writeLine(String back, String remove, boolean isAppend) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(back,isAppend));
        bw.write(remove);
        bw.newLine();
        bw.close();
    }

    public static ArrayList<String> readLine(String pathName) throws IOException {
        ArrayList<String> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader(pathName));
        String line;
        while ((line = br.readLine()) != null) {
            list.add(line);
        }
        br.close();
        return list;
    }

    public static void writeLine(String pathName, ArrayList<String> list, Boolean isAppend) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(pathName));
        for (String s : list) {
            bw.write(s);
            bw.newLine();
        }
        bw.close();
    }
}
